package com.dd.cloud.user.service.pres.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.PresConstants;
import com.dd.cloud.user.entity.pres.DrugsDiscount;
import com.dd.cloud.user.mapper.pres.DrugsDiscountMapper;
import com.dd.cloud.user.service.pres.IDrugsDiscountService;
import com.dd.cloud.common.utils.BigDecimalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author Lx
 * @version 1.0
 */
@Slf4j
@Service
public class DrugsDiscountServiceImpl extends ServiceImpl<DrugsDiscountMapper, DrugsDiscount> implements IDrugsDiscountService {
    /**
     * 获取用户药品折扣比例
     *
     * @param userId
     * @param userType
     * @return
     */
    @Override
    public Integer getDrugsDiscount(Integer userId, Integer userType) {
        QueryWrapper<DrugsDiscount> qw = new QueryWrapper<>();
        qw.lambda().eq(DrugsDiscount::getUserId, userId);
        qw.lambda().eq(DrugsDiscount::getUserType, userType);
        qw.lambda().eq(DrugsDiscount::getStatus, PresConstants.PresDrugsStatus.NOMER);
        DrugsDiscount drugsDiscount = getOne(qw);
//        if (drugsDiscount == null) {
//            qw.clear();
//            qw.lambda().eq(DrugsDiscount::getUserId, 0);
//            qw.lambda().eq(DrugsDiscount::getUserType, 0);
//            qw.lambda().eq(DrugsDiscount::getStatus, PresConstants.PresDrugsStatus.NOMER);
//            drugsDiscount = getOne(qw);
//        }
        if (drugsDiscount == null){
            return 100;
        }
        return drugsDiscount.getDiscount();
    }

    @Override
    public Double computeDiscountPrice(Double price, Integer discount) {
        double temp = price * discount;
        log.info("原价乘折扣价:{}", temp);
        temp = temp / 100;
        log.info("原价乘折扣价在除100:{}", temp);
        return temp;
    }

    @Override
    public BigDecimal computeDiscountPrice(BigDecimal price, Integer discount) {
        BigDecimal temp = BigDecimalUtils.multiply(price, BigDecimal.valueOf(discount));
        log.info("原价乘折扣价:{}", temp);
        temp = BigDecimalUtils.divide(temp, BigDecimal.valueOf(100));
        log.info("原价乘折扣价在除100:{}", temp);
        return temp;
    }
}
